/**
 * @file categories/dp/monotonous_queue/haoi2007/main.cpp
 * @brief 理想的正方形
 * @see
 * @author Ruiming Guo (guoruiming@stu.scu.edu.cn)
 * @copyright 2022
 * @date 2022/7/4 13:01:37
 **/

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1010, INF = 1e9;
int n, m, k;
int w[N][N];
int row_min[N][N], row_max[N][N];
int q[N];
void get_min(int a[], int b[], int tot) {
  int hh = 0, tt = 0;
  for (int i = 1; i <= tot; i++) {
    if (hh <= tt && q[hh] <= i - k) hh++;
    while (hh <= tt && a[q[tt]] >= a[i]) tt--;
    q[++tt] = i;
    b[i] = a[q[hh]];
  }
}
void get_max(int a[], int b[], int tot) {
  int hh = 0, tt = 0;
  for (int i = 1; i <= tot; i++) {
    if (hh <= tt && q[hh] <= i - k) hh++;
    while (hh <= tt && a[q[tt]] <= a[i]) tt--;
    q[++tt] = i;
    b[i] = a[q[hh]];
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &w[i][j]);
  for (int i = 1; i <= n; i++) {
    get_min(w[i], row_min[i], m);
    get_max(w[i], row_max[i], m);
  }
  int res = INF;
  int a[N], b[N], c[N];
  for (int i = k; i <= m; i++) {
    for (int j = 1; j <= n; j++) a[j] = row_min[j][i];
    get_min(a, b, n);
    for (int j = 1; j <= n; j++) a[j] = row_max[j][i];
    get_max(a, c, n);
    for (int j = k; j <= n; j++) res = min(c[j] - b[j], res);
  }
  cout << res << endl;
  return 0;
}
